package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.FtpConfig;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncPrdInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.enums.sync.PrdTypeEnum;
import com.pactera.madp.cp.common.util.sync.SFTPUtils;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.FundSftpConfig;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncFundInfoMapper;
import com.pactera.madp.cp.service.sync.SyncFundInfoHisService;
import com.pactera.madp.cp.service.sync.SyncFundInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class SyncFundInfoServiceImpl extends ServiceImpl<SyncFundInfoMapper, SyncPrdInfoEntity> implements SyncFundInfoService {

    private final SyncFundInfoHisService syncFundInfoHisService;
    private final SyncConfig syncConfig;
    private final FundSftpConfig fundSftpConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("基金产品：addYesterdayToHis");
        String date = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncFundInfoHisService.syncOneDay(date);
    }

    @Override
    public void deleteYesterday() {
        log.debug("基金产品：deleteYesterday");
        this.remove(Wrappers.<SyncPrdInfoEntity>lambdaQuery()
                .eq(SyncPrdInfoEntity::getPrdType, PrdTypeEnum.FUND.getPrdType()));
    }

    @Override
    public void parse() {
        log.debug("FTP获取数据文件");
        // 下载文件设置
        String srcFtpFile = SyncUtils.buildFilePath(syncConfig.getFundFtpFile(), Constants.FN_FUND, null);
        String destFile = SyncUtils.buildFilePath(syncConfig.getFundPath(), Constants.FN_FUND, null);
        // 下载目录检测
        String destDir = SyncUtils.buildFilePath(syncConfig.getFundPath(), "", null);
        boolean flag = SyncUtils.checkDir(destDir, true);
        if (!flag) {
            throw new RuntimeException("基金产品文件下载目录不存在");
        }
        // 文件下载
        FtpConfig ftpConfig = new FtpConfig();
        BeanUtil.copyProperties(fundSftpConfig, ftpConfig);
        flag = SFTPUtils.download(ftpConfig, srcFtpFile, destFile);
        if (!flag) {
            throw new RuntimeException("基金产品文件下载失败");
        }

        log.debug("基金产品：parse");
        File newestFile = SyncUtils.getNewestFile(destFile);
        if (ObjectUtil.isNull(newestFile)) {
            throw new RuntimeException("基金产品文件不存在");
        }
        List<SyncPrdInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_VERTICAL_LINE, SyncPrdInfoEntity.class, syncConfig.getFundCharset());

        entityList = speciaHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("基金产品：deleteHisOneDay");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncFundInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("基金产品：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 基金筛选
    private List<SyncPrdInfoEntity> speciaHandle(List<SyncPrdInfoEntity> entityList) {
        List<SyncPrdInfoEntity> newList = new ArrayList<>();
        entityList.forEach(e -> {
            if (StrUtil.equals("0", e.getPrdType())) {
                newList.add(e);
            }
        });
        return newList;
    }

}